﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Blog.TemplateEngine.Helpers
{
    public static class StringHelper
    {
        public static bool IsNumeric(this string s)
        {
            float value;
            return float.TryParse(s, out value);
        }

        public static string[] SplitAndTrim(this string s, char split)
        {
            var tmp = s.Split(new char[] { split }, StringSplitOptions.RemoveEmptyEntries);

            for (var i = 0; i < tmp.Length; i++)
            {
                tmp[i] = tmp[i].Trim();
            }

            return tmp;
        }

        public static string[] SmartSplitAndTrim(this string s, char split)
        {
            var ret = new List<string>();
            var pattern = string.Format(@"""(?:[^""\\]*(?:\\.[^""\\]*)*)""|[^\{0}]+", split);
            var regex = new Regex(pattern, RegexOptions.IgnorePatternWhitespace);
            foreach (Match m in regex.Matches(s))
            {
                var value = m.Value;
                if (value.StartsWith(@"""") && value.EndsWith(@""""))
                {
                    value = value.Replace(@"\""", @"""");
                }
                ret.Add(value.Trim());
            }

            return ret.ToArray();
        }

        public static IDictionary<string, IParameter> ParseNamedVariables(this string s, char seperator)
        {
            Dictionary<string, IParameter> ret = new Dictionary<string, IParameter>();

            string[] parameters = s.SmartSplitAndTrim(' ');
            for (int i = 1; i < parameters.Length; i++)
            {
                string[] pieces = parameters[i].SmartSplitAndTrim(seperator);
                if (pieces.Length != 2)
                {
                    throw new TimeoutException(String.Format("Invalid named variable command string: \"{0}\"", s));
                }
                IParameter param;
                string name = pieces[0];
                string value = pieces[1];
                if(value.StartsWith(@"""") && value.EndsWith(@""""))
                {
                    param = new StaticParameter(value.Substring(1, value.Length - 2));
                }
                else if(value.StartsWith(@"""") || value.EndsWith(@""""))
                {
                    throw new TimeoutException(String.Format("Invalid named variable command string: \"{0}\"", s));
                }
                else if (Char.IsNumber(value, 0))
                {
                    param = new StaticParameter(value);
                }
                else
                {
                    param = new VariableParameter(value);
                }

                ret[name] = param;
            }

            return ret;
        }

        public static Dictionary<string, object> ParseCommaSeperatedPairs(this string s)
        {
            Dictionary<string, object> ret = new Dictionary<string,object>();
            string[] pairs = s.SmartSplitAndTrim(',');
            foreach (string pair in pairs)
            {
                string[] values = pair.SmartSplitAndTrim('=');
                ret.Add(values[0], values[1]);
            }
            return ret;
        }
    }
}
